package com.chikitbd.common;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class PropertiesLoader {


    public final static String CONFIG = "config.properties";
    public final static String GUI_N_CONFIG = "guiNConfig.json";


    public static Properties p = new Properties();
    public static String V2RAY_CONFIG_CLASS_PATH = "";
    public static String V2RAY_CONFIG_WORKSPACE_PATH = "";

    public static String SHADOWSOCKSR_PATH = "";
    public static String SHADOWSOCKSR_URL = "";

    public static String SHADOWSOCKS_PATH = "";
    public static String SHADOWSOCKS_URL = "";

    public static String V2RAY_PATH = "";
    public static String V2RAY_URL = "";

    public static String SET_V2RAY_ISHADOWSOCKS = "";


    public static List<String> SERVER_INFOS = new ArrayList<String>();

    static {

//        System.out.println(System.getProperty("java.class.path"));
//        System.out.println(System.getProperty("user.dir"));


        // 程序编译目录
        String classpath = Thread.currentThread().getContextClassLoader()
                .getResource(PropertiesLoader.CONFIG).getPath();
        System.out.println("classpath: " + classpath);
        V2RAY_CONFIG_CLASS_PATH = Thread.currentThread().getContextClassLoader()
                .getResource(PropertiesLoader.GUI_N_CONFIG).getPath();
        ;

        // 程序工作目录
        String workspacePath = System.getProperty("user.dir");
        System.out.println("workspacePath:" + workspacePath + File.separator + PropertiesLoader.GUI_N_CONFIG);
        V2RAY_CONFIG_WORKSPACE_PATH = workspacePath + File.separator + PropertiesLoader.GUI_N_CONFIG;

        File f = new File(classpath);
        try (InputStream is = new FileInputStream(f)) {
            p.load(new InputStreamReader(is, "UTF-8"));
            for (String tmp : p.stringPropertyNames()) {
//                System.out.println(p.getProperty(tmp));
                if(tmp.startsWith("serverInfos")){
                    PropertiesLoader.SERVER_INFOS.add( p.getProperty(tmp));
                }
            }

            /**
             * 从config.properties文件中读取相关的配置参数
             */
            PropertiesLoader.V2RAY_PATH = p.getProperty("v2ray.folder");
            PropertiesLoader.V2RAY_URL = p.getProperty("v2ray.url");
            PropertiesLoader.SHADOWSOCKSR_PATH = p.getProperty("ShadowsocksR.folder");
            PropertiesLoader.SHADOWSOCKS_PATH = p.getProperty("Shadowsocks.folder");
            PropertiesLoader.SHADOWSOCKSR_URL = p.getProperty("Shadowsocks.url");
            PropertiesLoader.SHADOWSOCKS_URL = p.getProperty("Shadowsocks.url");
            PropertiesLoader.SET_V2RAY_ISHADOWSOCKS = p.getProperty("ShadowsocksR.setV2RayIShadowsocks");


        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e);
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e);
            System.exit(0);
        }
    }

    /**
     * @return
     */
    public static Properties getProperties() {
        return p;
    }

}
